﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Direct.Web.Mvc.Controls2;
using Direct.Core;
using Direct.Security;

namespace Direct.Mvc
{
    public class ServerControllerBase : Controller
    {
        protected ViewResult ProcessView(HtmlObject control, bool partial, Object dataContext = null)
        {
            ViewResult result = new ViewResult();
            result.ViewName = partial ? "RenderViewPartial" : "RenderView";
            result.ViewData = this.ViewData;
            if (dataContext != null)
                ViewData["__globalDataContext"] = dataContext;
            if (ViewData["__globalDataContext"] != null)
                control.DataContext = ViewData["__globalDataContext"];

            RenderOutputWriter renderer = new RenderOutputWriter(this, ViewData);
            // renderizar o controle
            renderer.Render(control);

            ViewData["__render_output"] = renderer;
            return result;
        }

        public ViewResult View(HtmlObject control, Object dataContext = null)
        {
            return ProcessView(control, false, dataContext);
        }

        public ViewResult View<TViewType>(Object dataContext = null) where TViewType : HtmlObject, new()
        {
            return ProcessView(new TViewType(), false, dataContext);
        }

        public ViewResult Partial(HtmlObject control, Object dataContext = null)
        {
            return ProcessView(control, true, dataContext);
        }

        public ViewResult Partial<TViewType>(Object dataContext = null) where TViewType : HtmlObject, new()
        {
            return ProcessView(new TViewType(), true, dataContext);
        }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            Assert.NullArgument(filterContext, "filterContext");
            // var controller = filterContext.Controller.ValueProvider["controller"].RawValue.ToString();
            // var action = filterContext.Controller.ValueProvider["action"].RawValue.ToString();
            var isAuthorized = ServiceLocator.GetInstance<IAuthorizationService>();
        }
    }
}
